from django.shortcuts import render
from django.http.response import JsonResponse

# Create your views here.
from myapp import models
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView,ListAPIView,ListCreateAPIView
from rest_framework import mixins
from rest_framework.viewsets import ViewSet
class PublisherModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.Publisher
        fields = "__all__"
        # fields = []
        # exclude = []
        # read_only_fields=[]
        # extra_kwargs = {
        #     'bread':{'min_value':0,}
        # }

class BookModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Book
        fields = "__all__"


class AuthorModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Author
        fields = "__all__"


class PublisherView(APIView):

    def get(self,request):

        pub_list = models.Publisher.objects.all()
        pub_ser = PublisherModelSerializer(pub_list,many=True)
        # 如果序列化的是多个queryset对象，就要many=Ture
        return Response(pub_ser.data)


    def post(self,request):
        print(request._request.body)
        print(request._request.POST)
        print("==========")
        print(request.data)
        pub_ser = PublisherModelSerializer(data=request.data)
        if pub_ser.is_valid():
            print(pub_ser.validated_data)
            pub_ser.save()
            return Response(pub_ser.data)
        else:
            return Response(pub_ser.errors)


class PublisherDetailView(APIView):


    def get(self,request,pk):

        pub_obj = models.Publisher.objects.filter(pk=pk).first()
        pub_ser = PublisherModelSerializer(pub_obj)
        return Response(pub_ser.data)

    def put(self,request,pk):
        pub_obj = models.Publisher.objects.filter(pk=pk).first()
        pub_ser = PublisherModelSerializer(pub_obj,data=request.data)
        if pub_ser.is_valid():
            pub_ser.save()
            return Response(pub_ser.data)
        else:
            return Response(pub_ser.errors)

    def delate(self,reuest,pk):
        models.Publisher.objects.filter(pk=pk).delate()
        return Response('删除成功')



class BookView(APIView):
    def get(self,request):
        print(request._request.body)
        print(request._request.GET)

        book_list = models.Book.objects.all()
        book_ser = BookModelSerializer(book_list,many=True)
        print(book_ser.data)
        return Response(book_ser.data)


    def post(self,request):
        print(request.data)
        book_ser = BookModelSerializer(data=request.data)
        if book_ser.is_valid():
            print(book_ser.validated_data)
            book_ser.save()
            return Response(book_ser.data)
        else:
            print(book_ser.errors)
            return Response(book_ser.errors)



class BookDetailView(APIView):


    def get(self,request,pk):
        book_boj = models.Book.objects.filter(pk=pk).first()
        book_ser = BookModelSerializer(book_boj,context=({"request":request}))
        print(type(book_ser.data))
        return JsonResponse(book_ser.data)




    def put(self,request,pk):

        book_obj = models.Book.objects.filter(pk=pk).first()
        book_ser = BookModelSerializer(book_obj,data=request.data)
        if book_ser.is_valid():
            book_ser.save()
            return Response(book_ser.data)
        else:
            return Response(book_ser.errors)


    def delate(self,request,pk):
        book_obj = models.Book.objects.filter(pk=pk).delete()
        return Response('删除成功')


# class PublisherGenericView(GenericAPIView):
#     queryset = models.Publisher.objects.all()
#     serializer_class = PublisherModelSerializer
#     def get(self,request):
#             qs = self.get_queryset()
#             serializers = self.get_serializer(qs,many=True)
#             return Response(serializers.data)
#
#
#             # d', 1), ('title', 'aaa'), ('price', 123), ('pub_date', '2020-12-23'), ('publisher', 1), ('authors', [])]),
#  OrderedDict([('id',2), ('title', 'bb'), ('price', 111), ('pub_date', '2020-12-23'), ('publisher', 1), ('authors', [])])]


class PublisherDetailGenericView(mixins.RetrieveModelMixin,mixins.DestroyModelMixin, GenericAPIView):
    queryset = models.Publisher.objects.all()
    serializer_class = PublisherModelSerializer

    def get(self, request, pk):
        # publisther = self.get_object()
        # serializer = self.get_serializer(publisther)
        # return Response(serializer.data)
        return self.retrieve(request)
    def put(self,request,pk):
        publisher = self.get_object()
        serializer = self.get_serializer(publisher,request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        else:
            return Response(serializer.errors())

    def delete(self,request,pk):
        self.destroy(request)
        return Response({'msg':"删除成功"})


class PublisherGenericView(ListAPIView):
    serializer_class = PublisherModelSerializer
    queryset = models.Publisher.objects.all()


# 视图集
